import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout'
import Cookies from 'js-cookie'
Vue.use(VueRouter)

const routes = [
    //====login====
  {
    path: '/login',
    name: 'Login',
    component: ()=>import('../../src/views/login/login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: ()=>import('../../src/views/login/register.vue')
  },

    //===前端页面===
  {
    path:'/hello',
    name:'hello',
    component:()=>import('../../src/views/hello'),
  },
  {
    path: '/going_fuzhou',
    name: 'going_fuzhou',
    component:()=>import('../views/going/going_fuzhou')
  },
  {
    path: '/zixun_fuzhou',
    name: 'zixun_fuzhou',
    component:()=>import('../views/going/zixun_fuzhou')
  },
  {
    path: '/tese_wenhua',
    name: 'tese_wenhua',
    component:()=>import('../views/going/tese_wenhua')
  },
  {
    path: '/tanshishan',
    name: 'tanshishan',
    component:()=>import('../views/tesewenhua/tanshishan')
  },
  {
    path: '/lvyou_zhinan',
    name: 'lvyou_zhinan',
    component:()=>import('../views/travel/lvyou_zhinan')
  },
  {
    path: '/details',
    name: 'details',
    component:()=>import('../views/travel/details')
  },
  {
    path: '/history',
    name: 'history',
    component:()=>import('../views/history/history')
  },
  {
    path: '/age',
    name: 'age',
    component:()=>import('../views/history/age')

  },
  {
    path: '/newFuzhou',
    name: 'newFuzhou',
    component: ()=>import('../../src/views/going/newFuzhou')
  },

    // ====后台页面====
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect:'/hello', //重定向
    children:[  //子路由
      // ====首页====
      {
        path: 'home',
        name: 'Home',
        component: ()=>import('../../src/views/home/HomeView.vue')
      },
      // ====User====
      {
        path: 'userList',
        name: 'UserList',
        component: ()=>import('../../src/views/user/User.vue')
      },
      {
        path: 'AddUser',
        name: 'AddUser',
        component: ()=>import('../../src/views/user/AddUser.vue')
      },
      {
        path: 'EditUser',
        name: 'EditUser',
        component: ()=>import('../../src/views/user/EditUser.vue')
      },
      // ====Admin====
      {
        path: 'adminList',
        name: 'AdminList',
        component: ()=>import('../../src/views/admin/Admin.vue')
      },
      {
        path: 'AddAdmin',
        name: 'AddAdmin',
        component: ()=>import('../../src/views/admin/AddAdmin.vue')
      },
      {
        path: 'EditAdmin',
        name: 'EditAdmin',
        component: ()=>import('../../src/views/admin/EditAdmin.vue')
      },
      // ====category====
      {
        path: 'CategoryList',
        name: 'CategoryList',
        component: ()=>import('../../src/views/category/Category.vue')
      },
      {
        path: 'AddCategory',
        name: 'AddCategory',
        component: ()=>import('../../src/views/category/AddCategory.vue')
      },
      {
        path: 'EditCategory',
        name: 'EditCategory',
        component: ()=>import('../../src/views/category/EditCategory.vue')
      },
      // ====category====
      {
        path: 'BookList',
        name: 'BookList',
        component: ()=>import('../../src/views/book/Book')
      },
      {
        path: 'AddBook',
        name: 'AddBook',
        component: ()=>import('../../src/views/book/AddBook')
      },
      {
        path: 'EditBook',
        name: 'EditBook',
        component: ()=>import('../../src/views/book/EditBook')
      },
      {
        path: 'background',
        name: 'background',
        component: ()=>import('../../src/views/going/background')
      },
      {
        path: 'TravelBackground',
        name: 'TravelBackground',
        component: ()=>import('../../src/views/travel/TravelBackground')
      },
      {
        path: 'HistoryBackground',
        name: 'HistoryBackground',
        component: ()=>import('../../src/views/history/HistoryBackground')
      },
      {
        path: 'MsgBackground',
        name: 'MsgBackground',
        component: ()=>import('../../src/views/travel/MsgBackground')
      },
      {
        path: 'EditGoing',
        name: 'EditGoing',
        component: ()=>import('../../src/views/going/EditGoing')
      },
      {
        path: 'AddTravel',
        name: 'AddTravel',
        component: ()=>import('../../src/views/travel/AddTravel')
      },
      {
        path: 'EditTravel',
        name: 'EditTravel',
        component: ()=>import('../../src/views/travel/EditTravel')
      },
      {
        path: 'EditHistory',
        name: 'EditHistory',
        component: ()=>import('../../src/views/history/EditHistory')
      },
      {
        path: 'newFuzhouBackground',
        name: 'newFuzhouBackground',
        component: ()=>import('../../src/views/going/newFuzhouBackground')
      },
      {
        path: 'AddnewFuzhou',
        name: 'AddnewFuzhou',
        component: ()=>import('../../src/views/going/AddnewFuzhou')
      },
      {
        path: 'EditnewFuzhou',
        name: 'EditnewFuzhou',
        component: ()=>import('../../src/views/going/EditnewFuzhou')
      },
      {
        path: 'teseBackground',
        name: 'teseBackground',
        component: ()=>import('../../src/views/tesewenhua/teseBackground')
      },
      {
        path: 'EditTese',
        name: 'EditTese',
        component: ()=>import('../../src/views/tesewenhua/EditTese')
      },

    ]
  },
  {
    path: '*',
    component:()=>import('@/views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// == 路由守卫 ==
router.beforeEach((to, from, next) => {
  if (to.path === '/hello') {
    next();
  } else if (to.path === '/login') {
    next();
  } else if (to.path==='/register'){
    next();
  }
  else {
      const admin=Cookies.get("admin")
      if (!admin && to.path!=='/login') return next('/login') //如果没有admin的数据并且跳转的路由并非login，强制返回到login
   // 访问home的时候，cookie当中存在数据，这个时候我就放行
    next()
  }
});

export default router
